home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC Answers 2002 January
/
PC Answers January 2002.7z
/
PC Answers January 2002.bin
/
graphics
/
freepixl
/
_SETUP.1
/
Com1com2.pxl
< prev
next >
Wrap
Text File
|
2001-10-11
|
10KB
|
318 lines
Initialize:
Title$ = "Serial Communications Control"
UseCaption(Title$)
UseCoordinates(PIXEL)
SetColorPalette(BITMAP)
UseBackground(TRANSPARENT,166,166,166)
DrawBackground
WinLocate(Title$,300,200,790,500,Res)
WinGetClientRect("",cx1,cy1,cx2,cy2)
DirGet(SourceDir$)
ImageDir$ = SourceDir$
WinShow(Title$,TOPMOST,Res)
InfoMenu(REMOVE)
WaitInput(100)
SetMenu("E&xit!",Leave,
ENDPOPUP,
"&Devices",IGNORE,
"Set COM port", SettingComms,
"Digitize", Digitize,
SEPARATOR,
"Interogate",Interogate,
ENDPOPUP)
ChangeMenuItem("Digitize",GRAY,Res)
Wait_for_Input:
WaitInput()
Leave:
End
Interogate:
DrawBackground
MessageBox(OKCANCEL,1,INFORMATION, "If no device is attached, PiXCL will wait with the hourglass cursor. Menus are still active. Do you want to continue?",
"PiXCL expects a device to be attached to the COM1 port",Res)
If Res = 1
SetCommPort(COM1,"baud=9600 parity=N data=8 stop=1",XON,Res)
WriteCommPort(COM1,"s")
UseCursor(WAIT)
WaitCommEvent(R,COM1,Bitpad3,0)
Endif
Goto Wait_for_Input
SettingComms:
DrawBackground
ModeParms$ = "baud=9600 parity=E data=7 stop=2"
SetCommPort(COM1,ModeParms$,XON,Res)
DebugMsgBox(Res)
If Res = 0 Then DrawText(10,10,"Set COM1 state Failed")
GetCommPort(COM1,Baud,DataBits,Parity,StopBits,XonXoff)
DrawNumber(10,30,Baud) DrawText(60,30,"baud rate")
DrawNumber(10,50,DataBits) DrawText(40,50,"data bits 4-8")
DrawNumber(10,70,Parity) DrawText(40,70,"parity 0-4=no,odd,even,mark,space")
DrawNumber(10,90,StopBits) DrawText(40,90,"stop bits 0,1,2 = 1, 1.5, 2")
DrawNumber(10,110,XonXoff) DrawText(40,110,"Xon/Xoff 1 = Xon")
ChangeMenuItem("Digitize",ENABLE,Res)
Goto Wait_for_Input
Digitize:
ClearCommPort(COM1)
Count = 0
Y1 = 10
{Next is a command for some digitizer pads. Consult your documentation.}
WriteCommPort(COM1,"Po")
UseCursor(WAIT)
WaitCommEvent(R,COM1,Bitpad2,0) {starts a com port watch thread}
Goto Wait_for_Input
Bitpad2:
UseCursor(ARROW)
If Count = 0 Then DrawBackground
ReadCommPort(COM1,BitpadData$)
Beep
Len(BitpadData$,Long)
If Long > 0
Long -= 2
Left(BitpadData$,Long,BitpadData$)
DrawText(10,Y1,BitpadData$)
Y1 += 20
Count++
Endif
If Count < 10
UseCursor(WAIT)
WaitCommEvent(R,COM1,Bitpad2,0)
Else
ClearCommPort(COM1)
Endif
Goto Wait_for_Input
Bitpad3:
ReadCommPort(COM1,BitpadData$)
Beep
UseCursor(ARROW)
Len(BitpadData$,Long)
If Long > 0
Long -= 2
Left(BitpadData$,Long,BitpadData$)
DrawText(10,10,BitpadData$)
Endif
ClearCommPort(COM1)
Goto Wait_for_Input
{
Serial Communications with PiXCL
PiXCL provides programmed read/write access to your PC serial ports, COM1, COM2, COM3
and COM4, for purposes such as reading data streams from input devices like digitising tables,
and commanding or reading devices that are controlled from an RS-232 port. These can be as
diverse as smart home appliances to model trains.
Most PCs have two serial ports only, COM1 and COM2, with COM2 often assigned to a modem
for Internet access. Some PCs use COM1 for the mouse input, while most newer PCs have a
PS2 style mouse port, which leaves COM1 available for other devices.
The PC design supports four standard serial ports which share only two interrrupt lines. Hence,
COM1 and COM3 share one, and COM2 and COM4 share the other. This means that if you want
install two serial cards and use two serial devices on COM1 and COM3, your software will have
to be able to support shared interrupts.
COM port support in PiXCL is not a device driver, and does not support shared interrupts.
Commands:
GetCommPort
Gets the current baudrate, parity etc
Syntax: GetCommPort(COMx,Baud,DataBits,Parity,StopBits,XonXoff)
Parameters:
COMx Port,where x = 1 - 4
Baud
DataBits
Parity
StopBits
XonXoff Data stream flow control. XOFF =0, XON = 1
======================================
SetCommPort
This command requires a string that specifies device-control information. The string must have
the same form as the mode commandÆs command-line arguments. For example, the following
string specifies a baud rate of 1200, no parity, 8 data bits, and 1 stop bit:
baud=1200 parity=N data=8 stop=1
The device name is ignored if it is included in the string, but it must specify a valid device, as follows:
COM1: baud=1200 parity=N data=8 stop=1
This command string can also be written with comma delimiters as
1200,N,8,1
For further information on mode command syntax, refer to the end-user documentation for your
operating system.
Syntax: SetCommPort(COMx,Mode$,XON|XOFF,Result)
Parameters:
COMx Port,where x = 1 - 4
Mode$ Port state parameters, according to the MS-DOS mode command, e.g.
"baud=9600 parity=N data=8 stop=2"
XON|XOFF Enables or disables the data stream control mode for both input and output.
Result 1 if the command was successful, otherwise 0.
Remarks:
You can set the COMx port parameters from the control panel for system wide operation. The settings
made within PiXCL only apply while PiXCL is running.
======================================
ClearCommPort
This command clears the selected comm port.
Syntax: ClearCommPort(COMx)
Parameters:
COMx Port,where x = 1 - 4
Remarks:
The same effect can be achieved by sending SetCommPort.
======================================
ReadCommPort - reads stream into string variable
Asynchronous data streams typically include variable length data, followed by a terminating
character or string. Most useful serial devices like digitizing tablets let you configure the
terminating string. In the current version of PiXCL, this string MUST be set to a carriage
return and linefeed pair (CR-LF).
PiXCL maintains a read buffer of about 4 KB. Most inout will be far less than this size. When the CR-LF
is received, a comms event is generated to which your PiXCL program can respond, in much the same
way that your program responds to mouse or keyboard events in the WaitInput() idle loop.
Hence, to receive a read or write comms event, you would use a command
WaitCommsEvent(R,<label>,Timeout)
at <label>, you would use a ReadCommPort command to get the current buffer.
Syntax: ReadCommPort(COMx,Data$)
Parameters:
COMx Port,where x = 1 - 4
Data$ The data read in from the port buffer
======================================
WriteCommPort
Asynchronous data streams typically include variable length data, followed by a terminating
character or string. Most useful serial devices like digitizing tablets let you configure the
terminating string. In the current version of PiXCL, this string MUST be set to a carriage
return and linefeed pair (CR-LF).
PiXCL maintains a write buffer of about 4 KB. When the last character in the buffer is written to the
port, a comms event is generated to which your PiXCL program can respond, in much the same way
that your program responds to mouse or keyboard events in the WaitInput() idle loop.
Hence, to receive a write comms event, you would use a command
WaitCommsEvent(W,<label>,Timeout)
Syntax: ReadCommPort(COMx,Data$)
Parameters:
COMx Port,where x = 1 - 4
Data$ The data read in from the port buffer. This includes the CR-LF terminator string, or may be
just a CR-LF. Once the buffer is read, it is cleared.
======================================
WaitCommEvent
Syntax: WaitCommEvent(R|W,Label,Timeout)
Parameters:
R | W R: wait for a CR-LF terminator to be received.
W: wait till the last character in the buffer is written. If the serial device written has to do
some processing, this may take a few seconds.
Label The jump-to location in your script to process the event.
Timeout The timelimit after which an event is issued anyway. This may happen if the attached
device is not responding, so your program will have to be able to handle this situation.
For example, if a Read timeout occurs, the buffer will be empty, and ReadCommPort will
return a NULL string. Timeout has a default value of 5000 mS (i.e. 5 minutes). If you set Timeout
to <= 0, it defaults to 5000.
Remarks:
WaitCommsEvent will interact with the WaitInput() idle loop. If a WaitCommsEvent is active, and a mouse
or keyboard event occurs, this will be processed immediately. You can set a flag variable to indictate that
a WaitCommsEvent is active, and put a WaitCommsEvent into a temporary idle loop.
Example using a digitizing tablet.
Step #1: Set the tablet baud rate and character framing, for example "9600,N,8,1", with XON flow control.
Step#2: Connect the tablet to the COM1 port on your PC. Ensure that the receive and transmit lines
(pins 2 and 3) connect PC receive to tablet transmit and vice-versa. If necessary, refer to the PC and
tablet documentation for more information.
Step#3: Insert a command in yoru PiXCL program like
SetCommPort(COM1,"baud=9600 parity=N data=8 stop=1",XON,Res) or
SetCommPort(COM1,"9600,N,8,1",XON,Res)
EscCommFunction
This commands sends escape functions to the selected COM port.
Syntax: EscCommFunction(COMx, ESC_token)
Parameters:
COMx Port,where x = 1 - 4
ESC_token
Specifies the code of the extended function to perform. This parameter can be one
of the following values:
Value Meaning
CLRDTR Clears the DTR (data-terminal-ready) signal.
CLRRTS Clears the RTS (request-to-send) signal.
SETDTR Sends the DTR (data-terminal-ready) signal.
SETRTS Sends the RTS (request-to-send) signal.
SETXOFF Causes transmission to act as if an XOFF character has been received.
SETXON Causes transmission to act as if an XON character has been received.
SETBREAK Suspends character transmission and places the transmission line
in a break state until the CLRREAK extended function code). Note that this extended
function does not flush data that has not been transmitted.
CLRBREAK Restores character transmission and places the transmission line in
a nonbreak state. The CLRBREAK extended function code is identical to the ClearCommPort
function.
}